import 'package:changxindianzishangwu/dao/user_info_dao.dart';
import 'package:changxindianzishangwu/model/user_info_model.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class SetEasyIdPage extends StatefulWidget {
  final String name;

  SetEasyIdPage({Key key, this.name}) : super(key: key);

  @override
  _SetEasyIdPageState createState() => _SetEasyIdPageState();
}

class _SetEasyIdPageState extends State<SetEasyIdPage> {
  @override
  Widget build(BuildContext context) {
    var easyId = "";
    return Scaffold(
      appBar: TopAppBar(context, "自语号", rightText: "完成", isRightTextBtn: true,
          rightCallback: () {
        if (easyId == "") {
          toast("请输入自语号");
          return;
        }
        if (!isAccount(easyId)) {
          toast("长度必须为6-12位，英文+数字组合");
          return;
        }
        EasyLoading.show(status: "修改中...");
        _updateUserInfo(easyId);
      }),
      body: Column(
        children: [
          InkWell(
            onTap: () {
              print(1);
            },
            child: Container(
              padding: EdgeInsets.only(left: 20),
              margin: EdgeInsets.only(
                  left: ScreenUtil().setWidth(15),
                  top: ScreenUtil().setHeight(10),
                  right: ScreenUtil().setWidth(15)),
              decoration: BoxDecoration(
                  color: Color.fromRGBO(241, 241, 241, 1),
                  borderRadius: BorderRadius.circular(6)),
              child: TextField(
                  cursorColor: Colors.lightBlueAccent,
                  keyboardType: TextInputType.name,
                  autofocus: true,
                  onChanged: (value) {
                    easyId = value;
                  },
                  decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: widget.name,
                      hintStyle: TextStyle(fontSize: 15)),
                  inputFormatters: [
                    ///8-16
                    WhitelistingTextInputFormatter(
                        RegExp("[a-zA-Z]|[0-9]")), //只能输入汉字或者字母或数字
                    LengthLimitingTextInputFormatter(16), //最大长度
                  ]),
            ),
          ),
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.only(right: 20, top: 10),
            child: Text(
              "一年仅可修改一次，请谨慎修改",
              style: TextStyle(
                fontSize: ScreenUtil().setSp(12),
                color: Color.fromRGBO(153, 153, 153, 1),
              ),
            ),
          ),
        ],
      ),
    );
  }

  _updateUserInfo(String id) async {
    try {
      UserInfoModel userInfoModel =
          await UserInfoDao.updateUserInfo("account", id);
      EasyLoading.dismiss();
      if (userInfoModel.resultCode == 0) {
        toast(userInfoModel.resultMsg);
        return;
      }
      Get.back(result: id);
      toast("修改成功");
    } catch (e) {
      print(e);
      EasyLoading.dismiss();
    }
  }

  static bool isAccount(String input) {
    RegExp mobile = new RegExp(r"(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$");
    return mobile.hasMatch(input);
  }
}
